package com.zrc.module_home.viewmodel

import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import com.zrc.module_home.viewmodel.model.DemoResponse
import com.zrc.superframe_base.mvx.mvvm.BaseViewModel
import com.zrc.module_home.HomeApi
import com.zrc.module_home.NetResponse
import com.zrc.superframe_net.interfac.NetBase
import com.zrc.superframe_net.common.net.ApplySchedulers
import com.zrc.superframe_net.common.net.NetObserver
import com.zrc.superframe_net.common.net.Net
import com.zrc.superframe_net.common.net.NetCallBack

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
class HomeVM(val page:NetBase) : BaseViewModel() {
    val demoLiveData = MutableLiveData<List<DemoResponse.OtherListBean>>()
    fun getData(page_num: Int) {
        Net
            .getService(HomeApi::class.java)
            .getData(20, page_num)
            .compose(ApplySchedulers())
            .subscribe(
                NetObserver(
                    page = page,
                    callback = object : NetCallBack<NetResponse<DemoResponse>>() {
                        override fun onSucceed(data: NetResponse<DemoResponse>, mothead: String) {
                            if (!data.data.other_list.isNullOrEmpty()) {
                                demoLiveData.value = data.data.other_list
                            } else {
                                demoLiveData.value = null
                            }
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            demoLiveData.value = null
                        }
                    })
            )
    }

    override fun attach(savedInstanceState: Bundle?) {

    }
}